/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Nav} from '../../utils/nav.js';
import {My} from '../my/my-model.js';
var nav = new Nav();
var my = new My();

Page({
    data: {
        isRuleTrue: false,
        userInfo: null,
        pageIndex: 1,
        loadingHidden: false,
        isLoadedAll: false,
        dataArr: [],
        totalPage: 0,
        param: {
            couponId: [],
            pageNo: 1,
            pageSize: 10
        }
    },
    onLoad: function() {
        this._findCoupon();
    },
    /**
     * 用户信息
     */
    _loadHead: function() {
      var that = this;
      // 查看是否授权
      wx.getSetting({
        success: function(res){
          if (res.authSetting['scope.userInfo']) {
            my.getUserInfo((data)=>{
              that.setData({
                userInfo: data
              })
            })
          } else {
            that.setData({
              isRuleTrue: true
            })
          }
        }
      })
    },
    /**
     * bind
     */
    onGotUserInfo: function (e) {
      var that = this;
      that.setData({
        isRuleTrue: false
      })
      if (e.detail.errMsg == 'getUserInfo:ok') {
        my.getUserInfo((data)=>{
          that.setData({
            userInfo: data
          })
        }, 1)
      } else {
        that.setData({
          isRuleTrue: true
        })
      }
    },
    /**
     * 获取优惠券数据
     */
    _findCoupon: function() {
        var that = this,
            param = this.data.param;
            param.pageNo = this.data.pageIndex;
        nav.findCoupon(param, (data)=>{
            data.list.forEach((item) => {
                item.per = (item.getNum/item.total*100).toFixed(2);
            })
            that.data.dataArr.push.apply(that.data.dataArr, data.list);
            that.setData({
                dataArr: that.data.dataArr,
                totalPage: data.totalPage,
                loadingHidden: true
            })
        })
    },
    /**
     * 领取优惠券
     */
    getCoupon: function(e) {
        var that = this;
        //校验登录
        if (that.data.userInfo==null) {
            that._loadHead();
        } else {
            //领取
            var id = nav.getDataSet(e, 'id'),
                param = that.data.param;
            param.couponId.push(id);
            nav.getCoupon(param, (data)=>{
                if(data.success){
                    wx.showToast({
                        title: data.message,
                        icon: 'none',
                        duration: 1000
                    })
                    that.data.dataArr = [];  // 初始化
                    that.data.pageIndex = 1;
                    that._findCoupon(() => {
                        that.setData({
                            isLoadedAll: false
                        });
                    })
                }
            })
        }
    },
    /**
     * 监听用户下拉动作 重新刷新用户收藏,回到&page=1
     */
    onPullDownRefresh: function() {
        var that = this;
        that.data.dataArr = [];  // 初始化
        that.data.pageIndex = 1;
        that._findCoupon(() => {
            that.setData({
                isLoadedAll: false
            });
            wx.stopPullDownRefresh()
        })
    },
    /**
     * 页面上拉触底事件的处理函数 上拉到底部触发&page=totalPage+1
     */
    onReachBottom: function() {
        if (!this.data.isLoadedAll && this.data.pageIndex < this.data.totalPage) {
            this.data.pageIndex++;
            this._findCoupon();
        }
        if (this.data.pageIndex == this.data.totalPage){
            this.setData({
                isLoadedAll: true
            })
        }
    }
})